package com.caowei.weicimpress.weicimpress.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.caowei.weicimpress.weicimpress.R;


/**
 * Created by caoweiPc on 2014/8/25.
 */
public  class ProgressView extends View {
    private final static String LOG=ProgressView .class.getSimpleName();
    Drawable drawable;
    private final int numofsnippet;
    private  final Handler mHandler = new Handler();
    private final int delayedMillisends;
    private  Runnable updateRunnable = new Runnable() {
        @Override
        public void run() {
            BEGIN+=STEP;
            if(BEGIN>=getWidth()){
                BEGIN=0;
            }
            invalidate();
            mHandler.postDelayed(updateRunnable, delayedMillisends);
        }
    };
    private final int STEP;
    private   int BEGIN=0;
    private boolean startAnimation=false;

    public ProgressView(Context context) {
        this(context,null);
    }

    public ProgressView(Context context, AttributeSet set) {
        super(context,set);
        drawable=getContext().getResources().getDrawable(R.drawable.progress) ;
        TypedArray a = context.obtainStyledAttributes(set,
                R.styleable.ProgressView);
        numofsnippet=a.getInt(R.styleable.ProgressView_numofsnippet,4);
        STEP=a.getInteger(R.styleable.ProgressView_step,50);
        delayedMillisends=a.getInteger(R.styleable.ProgressView_delayedMillisends,100);
        a.recycle();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        int snippet_w=getWidth()/numofsnippet;
        Rect bounds=new Rect(0,0,snippet_w,getHeight());
        if(BEGIN>0){
            int begin0=BEGIN;
            while (begin0>0){
                bounds.right=begin0;
                int left= begin0-snippet_w;
                bounds.left=left;
                drawable.setBounds(bounds);
                drawable.draw(canvas);
                begin0-=snippet_w;
            }
        }

        int begin=BEGIN;
        while (begin<getWidth()){
            bounds.left = begin;
            bounds.right = begin + snippet_w;
            drawable.setBounds(bounds);
            drawable.draw(canvas);
            begin+=snippet_w;
        }
        canvas.restore();
        if(!startAnimation){
            mHandler.postDelayed(updateRunnable,delayedMillisends);
            startAnimation=true;
        }
    }

/*
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width_mode=  MeasureSpec.getMode(widthMeasureSpec);
        int width=MeasureSpec.getSize(widthMeasureSpec);
        int heigh_mode=MeasureSpec.getMode(heightMeasureSpec);
        int heigh=MeasureSpec.getSize(heightMeasureSpec);

        switch (heigh_mode){
            case MeasureSpec.AT_MOST:
                Log.d(LOG,"MeasureSpec.AT_MOST");
                break;
            case MeasureSpec.EXACTLY:
                Log.d(LOG,"MeasureSpec.EXACTLY");
                break;
            case MeasureSpec.UNSPECIFIED:
                Log.d(LOG,"MeasureSpec.UNSPECIFIED");
                break;
        }
        setMeasuredDimension(getDefaultSize(SCCREN_WIDTH,widthMeasureSpec),getDefaultSize(BITMAP_HEIGH,
                heightMeasureSpec));
    }
*/

   
}